<?php

function daddslashes($string, $force = 0)
{
	!defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
	if (!MAGIC_QUOTES_GPC || $force)
	{
		if (is_array($string))
		{
			foreach ($string as $key => $val)
			{
				$string[$key] = daddslashes($val, $force);
			}
		}
		else
		{
			$string = addslashes($string);
		}
	}
	return $string;
}

function jsonShow($arr, $callback = 'callback', $show = true)
{
	require_once (ROOT_DIR . '/include/Services_JSON.php');
	$json = new Services_JSON();
	$enArr = $json->encode($arr);
	if ($show)
	{
		echo $enArr;
		exit();
	}
	return $enArr;
}

function getPicUrl($pic, $width, $height)
{
	$type = substr($pic, -4);
	$pic = str_replace($type, '_' . $width . 'x' . $height . $type, $pic);
	$url = $_SERVER['SINASRV_NDATA_CACHE_URL'] . APP_NAME . '/' . $pic;
	return $url;
}

function iconv_array($in_charset, $out_charset, $arr)
{
	if (strtolower($in_charset) == "utf8")
	{
		$in_charset = "UTF-8";
	}
	if (strtolower($out_charset) == "utf8")
	{
		$out_charset = "UTF-8";
	}
	if (is_array($arr))
	{
		foreach ($arr as $key => $value)
		{
			$arr[$key] = iconv_array($in_charset, $out_charset, $value);
		}
	}
	else
	{
		$arr = iconv($in_charset, $out_charset, $arr);
	}
	return $arr;
}

function getRealIpAddr()
{
	if (!empty($_SERVER['HTTP_CLIENT_IP']))
	{
		$ips = explode(',', $_SERVER['HTTP_CLIENT_IP']);
		foreach ($ips as $ip)
		{
			$ip = trim($ip);
			if (_validIP($ip) && !_isPrivate($ip))
				return $ip;
		}
	}
	if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
	{
		$ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
		foreach ($ips as $ip)
		{
			$ip = trim($ip);
			if (_validIP($ip) && !_isPrivate($ip))
				return $ip;
		}
	}

	return $_SERVER['REMOTE_ADDR'];
}

function check_api_key()
{

}

function array_merge_value($arr1, $arr2) {
    if(!is_array($arr1)) return array();

    if(!is_array($arr2))
        $arr2 = array($arr2);

    foreach($arr2 as $val) {
        if(!in_array($val, $arr1))
            $arr1[] = $val;
    }

    return $arr1;
}

function array_merge_exist($a1, $a2) {
    $tmp = array();

    foreach($a2 as $k => $v) {
        if(!empty($v))
            $tmp[$k] = $v;
    }

    return array_merge($a1, $tmp);
}
//全角作为一个字符
function get_length($str)
{
	$len = strlen($str);
	$strlen = $len;
	for($i = 0; $i < $len; $i++)
	{
		if(ord($str[$i])>128)
		{
			$strlen = $strlen-1;
			$i++;
		}
	}
	return $strlen;
}

/**
 * 获取客户端IP
 *
 * @return string
 */
function get_client_ip()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))
	{
		$ip = $_SERVER['HTTP_CLIENT_IP'];
	} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
	{
		$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
	} else {
	    $ip = $_SERVER['REMOTE_ADDR'];
	}
	return $ip;
}

/**
 * 获取用户IP
 * @return string
 */
function get_user_ip()
{
    if (!empty($_REQUEST['ip_addr'])) {
        return $_REQUEST['ip_addr'];
    }
    return $_SERVER['REMOTE_ADDR'];
}

/**
 * 循环创建目录
 *
 * @param string $dir
 * @param int $mode
 * @return boolean
 */
function mk_dir($dir, $mode = 0755)
{
    if (is_dir($dir) || @mkdir($dir,$mode))
    {
        return true;
    }
    if (!mk_dir(dirname($dir),$mode)) {
        return false;
    }
    return @mkdir($dir,$mode);
}


